﻿using HRX.PayCenter.ApplicationCore.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static HRX.PayCenter.ApplicationCore.Specifications.BizConst;

namespace HRX.PayCenter.ApplicationCore.Services
{
    /// <summary>
    /// 路由服务
    /// </summary>
    public class RouteService : IRouteService
    {
        private readonly IServiceProvider _serviceProvider;
        private readonly IAppLogger<RouteService> _appLogger;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceProvider"></param>
        public RouteService(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            _appLogger = _serviceProvider.GetRequiredService<IAppLogger<RouteService>>();
        }

        /// <summary>
        /// 获取支付服务
        /// </summary>
        /// <param name="routeKey"></param>
        /// <returns></returns>
        public IPayService GetPayService(string routeKey)
        {
            var list = _serviceProvider.GetServices<IPayService>();
            var strategyService = list.Where(l => l.ToString().ToLower().Contains($"{routeKey.ToLower()}tradeservice")).FirstOrDefault();
            return strategyService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public IPayService GetPayServiceByOrderNo(string orderNo)
        {
            string routeKey = "";
            if (orderNo.StartsWith(OrderNoFlag.AlipayFlag))
            {
                routeKey = "Alipay";
            }

            return GetPayService(routeKey);
        }
    }
}
